﻿/*
 *Author: Jeff Liu 
 *
 *Under MIT License
 *jebberwocky@gmail.com
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CtpGglMap.Core.StaticMap;
using CtpGglMap.Core;
using CtpGglMap.Util;

namespace CtpGglMap.Impl
{
    public class GMarker: IMapItem
    {

        public GeoPoint Point { set; get; }

        public String Label { set; get; }

        public String Size { set; get; }

        public String Color { set; get; }

        private IMap MapRef = null;

        public void SetMap(IMap map)
        {
            if (map != null)
            {
                MapRef = map;
                map.AddItem(this);
            }
        }

        /// <summary>
        /// The markers parameter takes set of value assignments (marker descriptors) of the following format:
        /// markers=markerStyles|markerLocation1| markerLocation2|... etc.
        /// </summary>
        /// <returns></returns>
        public String GetUrl()
        {
            StringBuilder sb = new StringBuilder();

            List<String> param = new List<string>();

            if (!string.IsNullOrEmpty(Label))
                param.Add("label:" + Label);

            if (!string.IsNullOrEmpty(Size))
                param.Add("size:" + Size);

            if (!string.IsNullOrEmpty(Color))
                param.Add("color:" + Color);

            //Location info has to be in end
            if (Point == null)
                return null;
            param.Add(Point.Lat + "," + Point.Lng);

            for (int i = 0; i < param.Count; i++)
            {
                sb.Append(param[i]);
                if (i < param.Count - 1)
                    sb.Append(Const.PIPE_SEPARATOR);
            }

            return sb.ToString();
        }

        /// <summary>
        /// Get GMarker's pixel coordinate as PixelXY [x,y]
        /// 
        /// refer to: Mika Tuupola's blog
        /// http://www.appelsiini.net/2008/6/clickable-markers-with-google-static-maps
        /// http://svn.appelsiini.net/svn/javascript/trunk/google_maps_nojs/Google/Maps.php
        /// http://www.appelsiini.net/2008/11/introduction-to-marker-clustering-with-google-maps
        /// </summary>
        public PixelXY PixelXY
        {
            get
            {
                if (this.MapRef != null)
                {
                    if (this.MapRef.GetType() == typeof(StaticGMap) || this.MapRef.GetType().IsSubclassOf(typeof(StaticGMap)) )
                    {
                        StaticGMap map = (StaticGMap)this.MapRef;
                        int center_offset_x = (int)Math.Round((double)map.Width / 2);
                        int center_offset_y = (int)Math.Round((double)map.Height / 2);

                        int center_y = GeoUtil.LatToY(map.Center.Lat);
                        int center_x = GeoUtil.LongToX(map.Center.Lng);

                        int target_y = GeoUtil.LatToY(this.Point.Lat);
                        int target_x = GeoUtil.LongToX(this.Point.Lng);

                        int delta_x = (target_x - center_x) >>  (21 - map.Zoom);
                        int delta_y = (target_y - center_y) >> (21 - map.Zoom);

                        int marker_x = center_offset_x + delta_x;
                        int marker_y = center_offset_y + delta_y;

                        return new PixelXY(marker_x, marker_y);
                    }
                  
                }
                return null;
            }
        }
    }
}
